package thread;


//饿汉
class Singleton{
    private static Singleton instance = new Singleton();
    public static Singleton getInstance(){
        return instance;
    }
    private Singleton(){

    }
}


//懒汉
class Singleton2{
    private static  volatile Singleton2  instance = null;
    private static Object locker = new Object();
    public static Singleton2 getInstance(){
        if(instance == null){
            synchronized (locker){
                if(instance == null){
                    instance = new Singleton2();
                }
            }
        }
        return instance;
    }
    private Singleton2(){

    }
}
public class HW7 {
    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s1==s2);
        Singleton2 ss = Singleton2.getInstance();
        Singleton2 sss = Singleton2.getInstance();
        System.out.println(ss==sss);
    }
}